﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using DunSilverlight.Web.Comm;
using DunLibrary.Dun;
using System.ServiceModel.Activation;
using System.Data;
using NHibernate;

namespace DunSilverlight.Web.DunWcf
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“InvestigateRequestService”。
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class InvestigateRequestService :BaseService<InvestigateRequest>,IInvestigateRequestService
    {

        /// <summary>
        /// 批量审核/反审核外访记录
        /// </summary>
        /// <param name="jsonIds"></param>
        /// <param name="check"></param>
        /// <returns></returns>
        public string BatchCheck(string jsonIds, int userId, bool check)
        {
            try
            {
                using (ISession sess = HbmSessionFactory.OpenSession())
                {
                    IDbConnection cnn = sess.Connection;

                    IList<int> ids = Newtonsoft.Json.JsonConvert.DeserializeObject<IList<int>>(jsonIds);

                    string strIds = null;

                    foreach (int id in ids)
                    {
                        strIds += strIds == null ? id.ToString() : "," + id;
                    }

                    string sql = string.Format("update Dun_InvestigateRequest set IsAllow ={0},CheckerId={1},CheckTime={2} where CheckerId={3} and Id in ({4})", check ? 1 : 0,userId, check?"getDate()":"NULL", userId, strIds);

                    IDbCommand cmd = cnn.CreateCommand();
                    cmd.CommandText = sql;
                    int result = cmd.ExecuteNonQuery();

                    return PackJsonResult("true", result.ToString(), string.Empty);
                }
            }
            catch (Exception ex)
            {
                return PackJsonResult("false", "0", ex.Message);
            }
        }
    }
}
